package com.haiheng.voiceandbook.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;


import com.haiheng.voiceandbook.R;

import androidx.appcompat.app.AppCompatActivity;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * @Des:
 * @Author: hwz
 * @Date:2021/8/26 0026
 * @Version: 1.0
 */
public class BaseDialog extends Dialog {

    protected Context mContext;

    protected boolean pressBackToClose = false;

    protected CompositeDisposable mCompositeDisposable;

    protected OnDialogDismissListener listener;

    //不响应返回按钮
    private OnKeyListener keylistener = new OnKeyListener() {

        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
//            if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
//                BaseDialog.this.dismiss();
//                return true;
//            } else {
//                return false;
//            }
            return keyCode == KeyEvent.KEYCODE_BACK;
        }
    };

    public BaseDialog(Context context) {
        this(context, R.style.dialog_with_alpha_anim);
    }

    public BaseDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
        init();
    }

    private void init() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mCompositeDisposable = new CompositeDisposable();
    }

    public BaseDialog pressBackToClose(boolean enable) {
        pressBackToClose = enable;
        return this;
    }

    /**
     * 设置Dialog的位置全屏底部显示,在setContentView之后执行
     */
    protected void setGravity(int gravity) {
        setGravity(gravity, 0.6f);
    }

    protected void setGravity(int gravity, float dimAmount) {
        Window window = getWindow();
//        window.requestFeature(Window.FEATURE_NO_TITLE);
//        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
//        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = gravity;
        lp.dimAmount = dimAmount;
        window.setAttributes(lp);
    }


    protected void setGravity(int gravity, boolean full) {
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        lp.gravity = gravity;
        window.setAttributes(lp);
    }

    protected AppCompatActivity getActivity() {
        return (AppCompatActivity) mContext;
    }

    @Override
    public void show() {
        if (!pressBackToClose) {
            this.setOnKeyListener(keylistener);
        }
        //某些渠道要隐藏一些东西
        super.show();
//        if (mContext instanceof Activity) {
//            if (((Activity) mContext).isFinishing()) {
//                return;
//            } else {
//                if (!pressBackToClose) {
//                    this.setOnKeyListener(keylistener);
//                }
//                //某些渠道要隐藏一些东西
//                super.show();
////                DialogManager.getInstance().showDialog(getClass().getSimpleName());
//            }
//        }

    }

    @Override
    public void dismiss() {
        dispose();
//        DialogManager.getInstance().dismissDialog();
        super.dismiss();
        if (this.listener != null) {
            this.listener.onDismiss();
        }
    }

    public void dispose() {
        if (mCompositeDisposable != null) {
            mCompositeDisposable.dispose();
        }
        mCompositeDisposable = null;
    }

    public BaseDialog showWithDismissListener(OnDialogDismissListener onDismissListener) {
        this.listener = onDismissListener;
        return this;
    }

    //监听窗口消失
    public BaseDialog onDismissListener(OnDialogDismissListener onDismissListener) {
        this.listener = onDismissListener;
        return this;
    }

    public void add(Disposable disposable) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        mCompositeDisposable.add(disposable);
    }

    public interface OnDialogDismissListener {

        void onDismiss();

    }

}
